# encoding: utf-8
class MembersController < ApplicationController
  before_filter :get_project, :authenticate_user!
  before_filter :is_member_of_project?
  
  def index
    @members = Member.find_all_by_project_id(params[:project_id])

    respond_to do |format|
      format.html
      format.xml { render :xml => @members }
    end
  end

  def new
    @available_users = User.where("id not in (?)", Project.where(:id => params[:project_id]).first.users)
    if @available_users.empty?
      flash[:error] = t :member_allready_all, :scope => [:flash, :error]
      respond_to do |format|
        format.html {redirect_to(project_members_path(params[:project_id], params[:anlegen]))}
      end
    else
      if is_project_manager?
        @member = Member.new
        respond_to do |format|
          format.html
        end
      else
        flash[:error] = t :member_no_right_create, :scope => [:flash, :error]
        respond_to do |format|
          format.html {redirect_to(project_members_path(params[:project_id], params[:anlegen]))}
        end
      end
    end
  end

  def show
    @member= Member.where(:id => params[:id]).first

    respond_to do |format|
      format.html
      format.xml { render :xml => @member }
    end
  end

  def create
    @member = Member.new(params[:member])
    @member.project_id = params[:project_id]
    @test = Member.where(:project_id => params[:project_id], :user_id => @member.user_id).first
    if @test
      flash[:error] = t :member_allready_member, :scope => [:flash, :error]

      respond_to do |format|
        format.html { redirect_to :action => "index"}
      end
    elsif @member.save
      flash[:notice] = t :member_add_success, :scope => [:flash, :notice]
      respond_to do |format|
        format.html { redirect_to(project_member_path(params[:project_id], @member)) }
      end
    else
      flash.now[:error] = t :member_not_added, :scope => [:flash, :error]
      respond_to do |format|
        format.html { render :action => "new" }
      end
    end
  end

  def edit
    if is_project_manager?
      @member = Member.where(:id => params[:id]).first
    else
      flash[:error] = t :member_no_right_edit, :scope => [:flash, :error]
      respond_to do |format|
        format.html { redirect_to( project_member_path(params[:project_id], params[:id]))}
      end
    end
  end

  def update
    @member = Member.where(:id => params[:id]).first
    flash[:notice] = t :member_edit_success, :scope => [:flash, :notice]
    respond_to do |format|
      if @member.update_attributes(params[:member])
        format.html { redirect_to(project_members_path(params[:project_id])) }
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @member.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @member = Member.where(:id => params[:id]).first
    if is_project_manager?
      @member.destroy
      flash[:notice] = t :member_delete_success, :scope => [:flash, :notice]
      respond_to do |format|
        format.html { redirect_to(project_members_path(params[:project_id]))}
        format.xml { head :ok }
      end
    else
      flash[:error] = t :member_no_right_delete, :scope => [:flash, :error]
      respond_to do |format|
        format.html { redirect_to( project_members_path(params[:project_id]))}
        format.xml { render :xml => @member.errors, :status => :unprocessable_entity }
      end
    end
  end  

end
